import {
  eventStore
} from "../../store/recommendSong"
import {
  rankStore
} from "../../store/ranking"
import {
  getRecommendSong
} from "../../network/music"
import {
  playStore
} from "../../store/player"
Page({
  /**
   * 页面的初始数据
   */
  data: {
    songList: [],
    type: "",
    key: ""
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    // eventStore.onState("recommendSongs", this.handleSong)
    // console.log(options)
    this.setData({
      type: options.type
    })
    if (options.type == "ranking") {
      this.data.key = options.key
      rankStore.onState(options.key, this.handleSong)
    } else if (options.type == "recommendSongs") {
      eventStore.onState("recommendSongs", this.handleSong)
    } else if (options.type == "menu") {
      this.fetchSong(options.id)
    }
  },
  // 点击的同时获取歌单列表
  getSongList(e) {
    // console.log(this.data.songList.tracks);
    const index = e.currentTarget.dataset.index;
    // console.log(index);
    playStore.setState("playIndex", index)
    playStore.setState("playSongList", this.data.songList.tracks)
  },
  handleSong(value) {
    if (this.data.type == "recommendSongs") {
      value.name = "推荐歌单"
    }
    this.setData({
      songList: value
    })
  },
  async fetchSong(id) {
    const date = await getRecommendSong(id);
    // console.log(date);
    this.setData({
      songList: date.playlist
    })
  },
  // /**
  //  * 生命周期函数--监听页面卸载
  //  */
  onUnload() {
    if (this.data.type == "ranking") {
      rankStore.offState(this.data.key, this.handleSong)
    } else if (this.data.type == "recommendSongs") {
      eventStore.offState("recommendSongs", this.handleSong)
    }
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})